using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace DAL
{
    public static class CookieManager
    {
        public static string TryGetCookie(string name, string field, string defaultValue)
        {
            HttpCookie myCookie = HttpContext.Current.Request.Cookies[name];
            if (myCookie != null)
            {
                string cookieValue;
                if (!String.IsNullOrEmpty(field))
                {
                    cookieValue = myCookie.Values[field];
                }
                else
                {
                    cookieValue = myCookie.Value;
                }

                if (cookieValue == null)
                {
                    cookieValue = defaultValue;
                }

                return cookieValue;
            }
            else
            {
                return defaultValue;
            }
        }

        public static string TryGetCookie(string name, string defaultValue)
        {
            return TryGetCookie(name, null, defaultValue);
        }

        public static void SetCookie(string name, string field, string value, DateTime expiration)
        {
            HttpCookie myCookie = HttpContext.Current.Request.Cookies[name];
            if (myCookie == null)
            {
                myCookie = new HttpCookie(name);
            }
            
            if (!string.IsNullOrEmpty(field))
            {
                myCookie.Values[field] = value;
            }
            else
            {
                myCookie.Value = value;
            }

            myCookie.Expires = expiration;

            HttpContext.Current.Response.Cookies.Add(myCookie);
        }

        public static void SetCookie(string name, string value, DateTime expiration)
        {
            SetCookie(name, null, value, expiration);
        }
    }
}